   /**
    * Remove spaces on beggining and in the end of string
    */
   function trim(string) {
       var ltrim = /^\s+/g;
       var rtrim = /\s+$/g;
       string = string.replace(ltrim,'');
       string = string.replace(rtrim,'');
       return string;
   }
   /**
    * Count shipping price according to users data (AJAX)
    * @param reload <boolean> shows to reload page or not.
    * @return void
    */
    function countShipping(reload){
        var urlwebsite = $('#hid-main-wsurl').val();
        var shippingData = {
            firstname : trim($('#shipping-firstname').val()),
            lastname : trim($('#shipping-lastname').val()),
            email : trim($('#shipping-email').val()),
            address1 : trim($('#shipping-address1').val()),
            address2 : trim($('#shipping-address2').val()),
            city : trim($('#shipping-city').val()),
            state : $('#shipping-state').val(),
            zip : trim($('#shipping-zip').val()),
            country : trim($('#shipping-country').find('option:selected').text()),
            phone : trim($('#shipping-phone').val())
        };

        var shippAddr = '';
        shippAddr    += (shippingData.firstname != '' ? shippingData.firstname : '');
        shippAddr    += (shippingData.lastname != '' ? ' ' + shippingData.lastname + '<br />' : '<br />');
        shippAddr    += (shippingData.address1 != '' ? shippingData.address1 : '');
        shippAddr    += (shippingData.address1 != '' && shippingData.address2 != '' ? ', ' : '');
        shippAddr    += (shippingData.address2 != '' ? shippingData.address2 : '');
        shippAddr    += '<br />';
        shippAddr    += (shippingData.city != '' ? shippingData.city : '');
        shippAddr    += (shippingData.city != '' && (shippingData.state != '' || shippingData.zip != '') ? ', ' : '');
        shippAddr    += (shippingData.state != '' ? shippingData.state : '');
        shippAddr    += (shippingData.state != '' && shippingData.zip != '' ? ' ' : '');
        shippAddr    += (shippingData.zip != '' ? shippingData.zip : '');
        shippAddr    += '<br />';
        shippAddr    += (shippingData.country != '' ? shippingData.country : '');

        $.ajax({
            type : 'post',
            url : urlwebsite +'sys/backend_shipping/calculate/',
            dataType : 'json',
            data : ({
                shippingService : 'internal',
                firstname : trim($('#shipping-firstname').val()),
                lastname : trim($('#shipping-lastname').val()),
                email : trim($('#shipping-email').val()),
                address1 : trim($('#shipping-address1').val()),
                address2 : trim($('#shipping-address2').val()),
                city : trim($('#shipping-city').val()),
                state : $('#shipping-state').val(),
                zip : trim($('#shipping-zip').val()),
                country : $('#shipping-country').val(),
                phone : trim($('#shipping-phone').val())
            }),
            beforeSend : function(){
                $('#startmsg').html('Calculating shipping value...');
                $('#startmsg').show();
            },
            complete : function(){
                $("#startmsg").html("Your Shipping Cost has been calculated.");
                window.setTimeout('$("#startmsg").hide(500)', 5000);
            },
            success: function(shipping){
                //shipping = eval('('+response+')');
                if (shipping.type == 'pickup' || shipping.type == 'error'){
                    return;
                } else {
                    var newVal = shipping.price;
//                    var oldTotal  = Number($('#sub-total').text());
//                    var tax = (!isNaN($('#total-tax').text()) ? Number($('#total-tax').text()) : 0);
//                    newTotal = oldTotal + tax + newVal;
                    $('#shipping-value').text(''+newVal+'');
//                    $('#total-price-value').text(''+newTotal+'');
                    $('#credit-card-shipping').val(newVal);
                    $('input:hidden[name="shipping"]').val(newVal);
                    $('#shipping-info-form').slideUp('slow');
                    $('#shipping-info-title span').text('Your shipping address:');
                    $('#edit-shipping').show();
                    $('#checkout').hide();
                    //$('#checkout').html("Proceed to Checkout");
                    //document.getElementById('checkout').setAttribute("onclick", "$('.cart-pay').show('slow')");
                    $('#edit-shipping').after('<span id="current-shipping-address" style="display:block;margin-left:30px;text-transform:none;">'+shippAddr+'</span>');
                    $('.cart-pay').show('slow');
                }
            window.setTimeout('recountTotal()',1000);
            if (true === reload){
                    top.location.reload();
                }
            }
            });

    }

    /**
     * Shipping form validator
     * if form validated countShipping() called
     * 
     * @return void
     */
    function validateShippingForm(){
        var error = new Array();
        var shippingAddr = '';

        if (trim($('#shipping-firstname').val()) == '') {
            $('#shipping-firstname').css('border-color','red');
            error.push(1);
        } else {
            $('#shipping-firstname').css('border-color','#7c7c7c');
        }
        /*if (trim($('#shipping-lastname').val()) == '') {
            $('#shipping-lastname').css('border-color','red');
            error.push(1);
        } else {
            $('#shipping-lastname').css('border-color','#7c7c7c');
        }*/
        if (trim($('#shipping-address1').val()) == '') {
            $('#shipping-address1').css('border-color','red');
            error.push(1);
        } else {
            $('#shipping-address1').css('border-color','#7c7c7c');
        }
        /*if (trim($('#shipping-address2').val()) == '') {
            $('#shipping-address2').css('border-color','red');
            error.push(1);
        } else {
            $('#shipping-address2').css('border-color','#7c7c7c');
        }*/
        if (trim($('#shipping-city').val()) == '') {
            $('#shipping-city').css('border-color','red');
            error.push(1);
        } else {
            $('#shipping-city').css('border-color','#7c7c7c');
        }
        /*if ($('#shipping-state').val() == '') {
            $('#shipping-state').css('border-color','red');
            error.push(1);
        } else {
            $('#shipping-state').css('border-color','#7c7c7c');
        }*/
        if (trim($('#shipping-zip').val()) == '' || isNaN(trim($('#shipping-zip').val()))) {
            $('#shipping-zip').css('border-color','red');
            error.push(1);
        } else {
            $('#shipping-zip').css('border-color','#7c7c7c');
        }
        if ($('#shipping-country').val() == '') {
            $('#shipping-country').css('border-color','red');
            error.push(1);
        } else {
            $('#shipping-country').css('border-color','#7c7c7c');
        }

        if (($('#shipping-country').val() == 'US' || $('#shipping-country').val() == 'CA') && $('#shipping-state').val() == '') {
            $('#shipping-state').css('border-color','red');
            error.push(1);
        } else {
            $('#shipping-state').css('border-color','#7c7c7c');
        }

        if (error.length > 0) {
            return;
        }

        countShipping(false);
    }

    function copyShippingData(formname)
    {
        switch (formname) {
        case 'paypal-creditcard':
            $('#credit-card-firstname').val($('#shipping-firstname').val());
            $('#credit-card-lastname').val($('#shipping-lastname').val());
            $('#credit-card-email').val($('#shipping-email').val());
            $('#credit-card-billing-address1').val($('#shipping-address1').val());
            $('#credit-card-billing-address2').val($('#shipping-address2').val());
            $('#credit-card-country').val($('#shipping-country').val());
            $('#credit-card-state').html(loadStates($('#shipping-country').val()))
                                   .val($('#shipping-state').val());
            $('#credit-card-city').val($('#shipping-city').val());
            $('#credit-card-zip').val($('#shipping-zip').val());
            $('#credit-card-phone').val($('#shipping-phone').val());
            
            break;
        case 'form_quote':
            $('#cart-quote-name').val($('#shipping-firstname').val() + ' ' + $('#shipping-lastname').val());
            $('#cart-quote-email').val($('#shipping-email').val());
            $('#cart-quote-address1').val($('#shipping-address1').val());
            $('#cart-quote-address2').val($('#shipping-address2').val());
            $('#cart-quote-country').val($('#shipping-country').val());
            $('#cart-quote-state').html(loadStates($('#shipping-country').val()))
                                   .val($('#shipping-state').val());
            $('#cart-quote-city').val($('#shipping-city').val());
            $('#cart-quote-zip').val($('#shipping-zip').val());
            $('#cart-quote-phone').val($('#shipping-phone').val());

            break;

        default:
            break;
        }
    }